
package com.yuan.receiver;

import com.yuan.application.BaseActivity;
import com.yuan.unit.NetUtils;
import com.yuan.unit.ToastHelper;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class NetStateReceiver extends BroadcastReceiver {
    /**
     * -1代表第一次启动 无任何提示、 1代表上次的网络状态为没有网络 2代表上次网络为2G/3G 3代表上次网络为wifi
     */
    private static int lastNet = -1;
    private Context mContext = null;

    @Override
    public void onReceive(Context mContext, Intent intent) {
        // TODO Auto-generated method stub
        this.mContext = mContext;
        if (!NetUtils.isNetworkConnected(mContext))
        {
            whatToToast(1);
        }
        else
        {
            if (NetUtils.isWifiConnected(mContext))
            {
                whatToToast(3);
            }
            else
            {
                whatToToast(2);
            }
        }
    }

    private void whatToToast(int i) {
        // TODO Auto-generated method stub
        if (lastNet == -1)
        {
            lastNet = i;
            return;
        }
        if (BaseActivity.mActivityTime != 2)
        {// 没有在屏幕最前端
            return;
        }
        switch (i)
        {
            case 1:
                ToastHelper.getInstance().shortToast(mContext, "网络已断");
                break;
            case 2:
                ToastHelper.getInstance().shortToast(mContext, "正在使用手机流量,小心使用");
                break;
            case 3:
                ToastHelper.getInstance().shortToast(mContext, "WIFI链接,随意使用！");
                break;
        }
    }

}
